package com.frame.work.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frame.work.entity.TemperatureControlLog;
import com.frame.work.entity.dto.TemperatureControlLogRepDTO;
import com.frame.work.service.TemperatureControlLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/data/temperatureControl/log")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Validated
public class TemperatureControlLogController {
    private final TemperatureControlLogService temperatureControlLogService;

    @GetMapping("/get")
    public Map<String, Object> getHomeById(@NotBlank String gatewayMac, @NotBlank String deviceMac,
                                           Integer pageNum, Integer pageSize, Long time) {
        //参数一是当前页，参数二是每页个数
        IPage<TemperatureControlLog> list = temperatureControlLogService.getPage(gatewayMac, deviceMac, pageNum, pageSize, time);
        Map<String, Object> logRep = new HashMap<>(4);
        List<TemperatureControlLog> records = list.getRecords();
        List<TemperatureControlLogRepDTO> temperatureControlLogRepDtoList = new ArrayList<>(10);
        records.forEach(r -> {
            TemperatureControlLogRepDTO temperatureControlLogRepDTO = new TemperatureControlLogRepDTO().repDto(r);
            temperatureControlLogRepDtoList.add(temperatureControlLogRepDTO);
        });

        logRep.put("records", temperatureControlLogRepDtoList);
        logRep.put("total", list.getTotal());
        logRep.put("pageSize", list.getSize());
        logRep.put("pageNum", list.getCurrent());
        return logRep;
    }
}
